package initialize

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
	"xinxiang-mutual-platform/conf"
	"xinxiang-mutual-platform/global"
	"xinxiang-mutual-platform/router"
	"xinxiang-mutual-platform/utils"
)

var (
	R *gin.Engine
)

func Init() {
	// 初始化日志库
	if err := InitLogger(conf.AppMode); err != nil {
		zap.L().Error("初始化日志库失败")
	}
	zap.L().Info(utils.Green("初始化日志成功"))
	// 初始化数据库
	db, err := gorm.Open(mysql.Open(conf.DBdns), &gorm.Config{})
	if err != nil {
		panic("数据库初始化失败")
	}
	zap.L().Info(utils.Green("初始化日志库成功"))
	global.DB = db
	sqlDB, err := global.DB.DB()
	sqlDB.SetMaxIdleConns(10)  //设置空闲连接池中连接的最大数量
	sqlDB.SetMaxOpenConns(100) // 最大连接数数
	sqlDB.SetConnMaxLifetime(time.Second * 30)

	// 初始化路由引擎
	R = gin.New()
	R.Use(GinLogger(), GinRecovery(true))
	// 初始化路由
	sosRouter := router.RouterGroupApp.SOSRouterGroup.SOSRouter
	sosRouter.InitSOSRouter(R)
	IP_Port := conf.IP + conf.HttpPort
	R.Run(IP_Port)
}
